package com.chilli.cosima.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.TOperator;
import com.chilli.cosima.data.bean.TPower;
import com.chilli.cosima.data.bean.TPowerRole;
import com.chilli.cosima.data.bean.TRole;
import com.chilli.cosima.data.dao.UserDAO;
import com.chilli.cosima.service.UserService;

public class UserServiceImpl implements UserService
{
	private static final Log log = LogFactory.getLog(UserServiceImpl.class);

	private List<String> powerTreeList = new ArrayList<String>();

	private List<String> updatePowerTreeList = new ArrayList<String>();

	public Long createUser(TOperator user) throws BussinessException
	{
		try
		{
			return userDao.createUser(user);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

		return null;
	}

	public void deleteUser(Long[] id) throws BussinessException
	{
		try
		{
			userDao.deleteUser(id);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

	}

	public List<TOperator> findUserAll()
	{
		try
		{
			return userDao.findUserAll();
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

		return null;
	}

	public Object findUserById(Long id)
	{
		try
		{
			return userDao.findUserById(id);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

		return null;
	}

	public List<TOperator> findUserByRoleId(Long roleId)
	{
		try
		{
			return userDao.findUserByRoleId(roleId);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

		return null;
	}

	public TOperator findUserByName(String username)
	{
		List<TOperator> list = userDao.findUserByName(username);
		if (list == null || list.size() < 1)
		{
			return null;
		}

		return list.get(0);

	}

	@SuppressWarnings("unchecked")
	public Map findUserByPage(int current, int pageSize)
	{
		try
		{

			return userDao.findUserByPage(current, pageSize);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public Map findRoleByPage(int current, int pageSize)
	{
		try
		{

			return userDao.findRoleByPage(current, pageSize);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;

	}

	public void updateUser(TOperator user) throws BussinessException
	{
		try
		{
			userDao.updateUser(user);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
	}

	public List<TRole> findAllRoles()
	{
		try
		{
			return userDao.findAllRoles();
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public String findRoleNameById(long roleid)
	{
		try
		{
			return userDao.findRoleNameById(roleid);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public UserDAO getUserDao()
	{
		return userDao;
	}

	public void setUserDao(UserDAO userDao)
	{
		this.userDao = userDao;
	}

	private UserDAO userDao;

	public List<String> getPowerTreeList()
	{
		return powerTreeList;
	}

	public List<String> getUpdatePowerTreeList()
	{
		return updatePowerTreeList;
	}

	public List<TPower> findAllPowers()
	{
		try
		{
			return userDao.findAllPowers();
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;

	}

	public void clearPowerTreeList()
	{
		powerTreeList.clear();
	}

	public void clearUpdatePowerTreeList()
	{
		updatePowerTreeList.clear();
	}

	/**
	 * 写出权限树 <功能详细描述>
	 * 
	 * @param fatherid
	 *            fatherid
	 * @param powerlist
	 *            powerlist
	 * @see [类、类#方法、类#成员]
	 */
	public void createTree(String fatherid, List<TPower> powerlist)
	{

		// 每个用户的权限是从0开始的。所以首先从父权限中取得整个权限列表
		// 首先根据最上面的权限去查下面的权限
		List<TPower> tempList = new ArrayList<TPower>();
		TPower power = null;
		StringBuffer returStr = new StringBuffer();
		for (int i = 0; i < powerlist.size(); i++)
		{
			power = powerlist.get(i);
			if (fatherid.equals(power.getFatherid()))
			{
				tempList.add(power);
			}
		}
		if (tempList.size() > 0)
		{
			for (int i = 0; i < tempList.size(); i++)
			{
				power = (TPower) tempList.get(i);

				returStr.setLength(0);

				returStr.append("d.add('" + power.getPowerid() + "','"
						+ power.getPowername() + "','testCheck');");
				powerTreeList.add(returStr.toString());
				createTree(power.getPowerid(), powerlist);
			}
		}
	}

	/**
	 * 修改权限树 <功能详细描述>
	 * 
	 * @param optpowerlist
	 *            optpowerlist
	 * @param fatherid
	 *            fatherid
	 * @param powerlist
	 *            powerlist
	 * @see [类、类#方法、类#成员]
	 */
	public void updateTree(String fatherid, List<TPowerRole> optpowerlist,
			List<TPower> powerlist)
	{
		// 每个用户的权限是从0开始的。所以首先从父权限中取得整个权限列表
		// 首先根据最上面的权限去查下面的权限
		List<TPower> tempList = new ArrayList<TPower>();
		TPower power = null;
		StringBuffer returStr = new StringBuffer();
		for (int i = 0; i < powerlist.size(); i++)
		{
			power = powerlist.get(i);
			if (fatherid.equals(power.getFatherid()))
			{
				tempList.add(power);
			}
		}
		if (tempList.size() > 0)
		{
			for (int i = 0; i < tempList.size(); i++)
			{
				power = (TPower) tempList.get(i);

				if (isPower(power.getPowerid(), optpowerlist))
				{
					returStr.setLength(0);
					returStr.append("d.add('" + power.getPowerid() + "','"
							+ power.getPowername() + "','testCheck','true');");
					updatePowerTreeList.add(returStr.toString());
					updateTree(power.getPowerid(), optpowerlist, powerlist);
				} else
				{
					returStr.setLength(0);
					returStr.append("d.add('" + power.getPowerid() + "','"
							+ power.getPowername() + "','testCheck');");
					updatePowerTreeList.add(returStr.toString());
					updateTree(power.getPowerid(), optpowerlist, powerlist);

				}
			}
		}
	}

	/**
	 * 判断拥有哪些权限 <功能详细描述>
	 * 
	 * @param powerid
	 *            powerid
	 * @param optpowerlist
	 *            optpowerlist
	 * @return boolean true/false
	 * @see [类、类#方法、类#成员]
	 */
	public boolean isPower(String powerid, List<TPowerRole> optpowerlist)
	{
		if (null != optpowerlist && optpowerlist.size() > 0)
		{
			for (int i = 0; i < optpowerlist.size(); i++)
			{
				TPowerRole powerrole = optpowerlist.get(i);
				if (powerid.equals(powerrole.getPowerid()))
				{
					return true;
				}
			}
		}
		return false;
	}

	public TRole findRoleByName(String rolename)
	{
		try
		{
			return userDao.findRoleByName(rolename);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;

	}

	public Long createRole(TRole role) throws BussinessException
	{
		try
		{
			return userDao.createRole(role);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

		return null;
	}

	public Long createPowerRole(TPowerRole powerrole) throws BussinessException
	{
		try
		{
			return userDao.createPowerRole(powerrole);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

		return null;
	}

	public void createRoleSavePower(TRole role, String[] testCheck)
			throws BussinessException
	{
		try
		{
			// 创建角色
			Long roleid = userDao.createRole(role);

			// 保存对应权限
			for (int i = 0; i < testCheck.length; i++)
			{
				TPowerRole powerRole = new TPowerRole();

				powerRole.setRoleid(roleid);
				powerRole.setPowerid(testCheck[i]);

				// 保存权限
				userDao.createPowerRole(powerRole);

			}
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
	}

	public void deleteRole(Long[] id) throws BussinessException
	{
		try
		{
			userDao.deleteRole(id);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

	}

	public void deleteRolePower(Long[] id) throws BussinessException
	{
		try
		{
			userDao.deleteRolePower(id);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}

	}

	public void deleteRoleAndPower(Long[] id) throws BussinessException
	{
		try
		{
			userDao.deleteRole(id);
			userDao.deleteRolePower(id);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
	}

	public void updateRole(TRole role) throws BussinessException
	{
		try
		{
			userDao.updateRole(role);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
	}

	public TRole findRoleById(long roleid)
	{
		try
		{
			return userDao.findRoleById(roleid);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public List<TPowerRole> findRolePowerByRoleId(Long roleid)
			throws BussinessException
	{
		try
		{
			return userDao.findRolePowerByRoleId(roleid);
		} catch (RuntimeException e)
		{
			log.error(e.getMessage(), e);
		}
		return null;
	}
}
